package cn.hs.service.impl;

import cn.hs.entity.Course;
import cn.hs.entity.CourseType;
import cn.hs.mapper.*;
import cn.hs.pojo.CourseIdAndName;
import cn.hs.pojo.CourseSelect;
import cn.hs.service.CourseService;
import cn.hs.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @ClassName CourseServiceImpl
 * @Description TODO  课程业务层
 * @Author yun
 * @Date 2024年10月22日 21:19
 * @Version 1.0
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    @Qualifier("courseMapper")
    private CourseMapper mapper;
    @Autowired
    private CourseMarkMapper markMapper;
    @Autowired
    private CourseTypeMapper typeMapper;
    @Autowired
    private ConsultMapper consultMapper;
    @Autowired
    private UserCourseMapper userCourseMapper;

    @Override
    public Result add(Course course) {
        if(course == null){
            return Result.error("添加数据为空！");
        }
//        判断关联表信息
//        courseType的id
        if(typeMapper.selectById(course.getTypeId()) == null){
            return Result.error("课程类型不存在!");
        }
//        consult的id


        if(mapper.add(course) == 1){
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result delete(Integer id) {
        if(id == null){
            return Result.error();
        }
//        先判断是否存在课程
        if(mapper.selectById(id) == null){
            return Result.error("课程不存在");
        }

////        先删除子表信息
////        courseMark
//        markMapper.deleteByCourseId(id);
////        courseTalk
//
////        courseReply
//        if(mapper.delete(id) == 1){
//            return Result.success();
//        }

//        判断有无关联会话
        if(markMapper.selectCountByCourseId(id) == 0){
            if(mapper.delete(id) == 1){
                return Result.success();
            }else{
                return Result.error("课程有关联会话");
            }
        }
        return Result.error("删除课程失败！");
    }

    @Override
    public Result update(Course course) {
        if(course == null){
            return Result.error("更新的数据为空！");
        }
        if(mapper.update(course) == 1){
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result selectAll(CourseSelect select) {
//
        if(select != null){
            if(select.getPageNo()!= null && select.getPageSize() != null){
                PageHelper.startPage(select.getPageNo(), select.getPageSize());
            }
        }else{
            select = CourseSelect.builder()
                    .pageNo(1)
                    .pageSize(10)
                    .build();
        }
        List<Course> list = mapper.selectAll(select);
//        添加外键信息
//        for (int i = 0; i < list.size(); i++) {
//            Course course = list.get(i);
////            添加type信息
//            if(course.getTypeId() != null){
//                list.get(i).setType(typeMapper.selectById(course.getTypeId()));
//            }
////            添加咨询师信息
//            if(course.getTeacherId() != null){
//                list.get(i).setConsult(consultMapper.selectByPrimaryKey(Long.valueOf(course.getTeacherId())));
//            }
//        }


//        填充pjCount
        for (int i = 0; i < list.size(); i++) {
            Integer id = list.get(i).getId();
//        评论次数
            list.get(i).setPjCount(markMapper.selectCountByCourseId(id));
//        收藏次数
            list.get(i).setCollects(userCourseMapper.selectCountByCourseId(id));
        }

//        排序
//        if(select.getOrder().equals("name")){
//
//        }
        if(select.getOrder() != null){
            if(select.getOrder().equals("pjCount")){
                list.sort((o1, o2) -> o2.getPjCount().compareTo(o1.getPjCount()));
            }
            if(select.getOrder().equals("userCourse")){
                list.sort((o1, o2) -> o2.getCollects().compareTo(o1.getCollects()));
            }
        }

        PageInfo<Course> info = new PageInfo<>(list);
        return Result.success(info);

    }

    @Override
    public Result selectById(Integer id) {
        if(id == null){
            return Result.error("id为空！");
        }
        Course course = mapper.selectById(id);
        if(course == null){
            return Result.error("查询数据为空！");
        }
        return Result.success(course);
    }

    @Override
    public Result selectAllIdAnName(CourseSelect select) {
        if(select != null){
            if(select.getPageNo()!= null && select.getPageSize() != null){
                PageHelper.startPage(select.getPageNo(), select.getPageSize());
            }
        }
        List<CourseIdAndName> list = mapper.selectIdAndName(select);
        PageInfo<CourseIdAndName> info = new PageInfo<>(list);
        return Result.success(info);
    }

    @Override
    public PageInfo selectByTeacherIdCourses(Integer teacherId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Course> courses = mapper.selectByTeacherIdCourses(teacherId);
        PageInfo<Course> info = new PageInfo<>(courses);
        return info;

    }


}
